package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.dto.SmsProductDto;
import com.mall.entity.PmsProduct;
import com.mall.entity.SmsNewCommodity;
import com.mall.entity.SmsSpikeCommodity;
import com.mall.service.PmsProductService;
import com.mall.service.SmsSpikeCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "SmsCouponCategoryController", description = "秒杀活动与商品关系管理")
@RestController
@RequestMapping("/spikeCommodities")
public class SmsSpikeCommodityController {

    @Resource
    private SmsSpikeCommodityService smsSpikeCommodityService;

    @Resource
    private PmsProductService pmsProductService;

    @ApiOperation(value = "新增秒杀活动与商品关系")
    @PostMapping
    public Result save(@RequestBody SmsSpikeCommodity smsSpikeCommodity) {
        boolean flag = smsSpikeCommodityService.save(smsSpikeCommodity);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除秒杀活动与商品关系")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsSpikeCommodityService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查询秒杀活动与商品关系")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsSpikeCommodity smsSpikeCommodity = new SmsSpikeCommodity();
        smsSpikeCommodity = smsSpikeCommodityService.getById(id);
        Integer code = smsSpikeCommodity !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsSpikeCommodity !=null ? "successful":"failed,please try again";
        return new Result(smsSpikeCommodity,code,msg);
    }

    @ApiOperation(value = "根据活动id和时间段id查询商品关系")
    @GetMapping("/list")
    public Result getList(@RequestParam Integer promotionId,
                          @RequestParam Integer sessionId) {
        List<SmsSpikeCommodity> list = smsSpikeCommodityService.getBatch(promotionId, sessionId);
        List<SmsProductDto> productList = new ArrayList<>();
        for(int i=0;i< list.size();i++)
        {
            SmsProductDto result = new SmsProductDto();
            PmsProduct product = pmsProductService.getById(list.get(i).getProductId());
            result.setId(list.get(i).getId());
            result.setName(product.getName());
            result.setProductSn(product.getProductSn());
            result.setPrice(product.getPrice());
            result.setStock(product.getStock());
            result.setPromotionPrice(list.get(i).getFlashPromotionPrice());
            result.setPromotionCount(list.get(i).getFlashPromotionCount());
            result.setPromotionLimit(list.get(i).getFlashPromotionLimit());
            result.setSort(list.get(i).getSort());
            productList.add(result);

        }
        Integer code = productList!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = productList!=null ? "successful":"failed,please try again";
        return new Result(productList,code,msg);
    }

    @ApiOperation(value = "查询全部秒杀活动与商品关系")
    @GetMapping
    public Result getAll(){
        List<SmsSpikeCommodity> list = smsSpikeCommodityService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "批量增加秒杀商品列表")
    @GetMapping("/addlist")
    public Result addList(
            @RequestParam Integer id,
            @RequestParam Integer promotionId,
            @RequestParam Integer sessionId){
        PmsProduct product = pmsProductService.getById(id);
        SmsSpikeCommodity smsSpikeCommodity = new SmsSpikeCommodity();
        smsSpikeCommodity.setProductId(product.getId());
        smsSpikeCommodity.setFlashPromotionId(promotionId);
        smsSpikeCommodity.setFlashPromotionSessionId(sessionId);
        smsSpikeCommodity.setSort(0);
        boolean flag = smsSpikeCommodityService.save(smsSpikeCommodity);
        return new Result(flag?Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsSpikeCommodityService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsSpikeCommodity> getPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String title) {
        IPage<SmsSpikeCommodity> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsSpikeCommodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return smsSpikeCommodityService.page(page,queryWrapper);
    }
}
